﻿/*++

Copyright (c) 2011 Rainr Corporation

Module Name:

	Rainr.Utilities.IO.XmlDocumentEx

Abstract:

	XmlDocument扩展

Author:

	Zhou Junyu (Rainr)    2011-07-04

Revision History:
  
--*/

using System.Xml;
using System.IO;
using System;

namespace Rainr.Utilities.IO
{
    /// <summary>
    /// XMLDocument扩展
    /// </summary>
    public class XmlDocumentEx : XmlDocument
    {
        /// <summary>
        /// 获取单个节点
        /// </summary>
        /// <param name="parentNode">父节点</param>
        /// <param name="xpath">子节点XPath</param>
        /// <returns>子节点</returns>
        public XmlNode SelectSingleNode(XmlNode parentNode, string xpath)
        {
            if (null == parentNode) return null;
            return parentNode.SelectSingleNode(xpath);
        }
        /// <summary>
        /// 获取XML文件根节点下的子节点集合
        /// </summary>
        /// <returns>子节点集合</returns>
        public XmlNodeList SelectRootChildNodes()
        {
            return this.DocumentElement.ChildNodes;
        }

        /// <summary>
        /// 获取节点元素的属性
        /// </summary>
        /// <param name="xmlElement">节点元素</param>
        /// <param name="attributeName">属性名称</param>
        /// <returns>成功：属性值对象，失败：NULL</returns>
        public XmlAttribute GetAttribute(XmlElement xmlElement, string attributeName)
        {
            if (null == xmlElement) return null;
            return xmlElement.GetAttributeNode(attributeName);
        }
        /// <summary>
        /// 获取属性对象
        /// </summary>
        /// <param name="xmlNode">XML节点</param>
        /// <param name="attributeName">属性名称</param>
        /// <returns>成功： XML属性对象，失败：NULL</returns>
        public XmlAttribute GetAttribute(XmlNode xmlNode, string attributeName)
        {
            if (null == xmlNode) return null;
            return xmlNode.Attributes[attributeName];
        }
        /// <summary>
        /// 获取节点元素的属性值
        /// </summary>
        /// <param name="xmlElement">节点元素</param>
        /// <param name="attributeName">属性名称</param>
        /// <returns>成功：属性值，失败：NULL</returns>
        public string GetAttributeValue(XmlElement xmlElement, string attributeName)
        {
            if (null == xmlElement) return null;
            return xmlElement.GetAttribute(attributeName);
        }
        /// <summary>
        /// 获取节点的属性值
        /// </summary>
        /// <param name="xmlNode">XML节点</param>
        /// <param name="attributeName">节点属性名称</param>
        /// <returns>成功：属性值，失败：NULL</returns>
        public string GetAttributeValue(XmlNode xmlNode, string attributeName)
        {
            XmlAttribute xa = this.GetAttribute(xmlNode, attributeName);
            if (null == xa) return null;
            return xa.Value;
        }

        /// <summary>
        /// 加载Xml文档
        /// </summary>
        /// <param name="filePath">文件绝对路径</param>
        /// <returns>一个Xml文档扩展对象 (XmlDocumentEx)</returns>
        public static XmlDocumentEx LoadXmlDocument(string filePath)
        {
            XmlDocumentEx xmlEx = null;
            if (!File.Exists(filePath)) return xmlEx;
            try
            {
                xmlEx = new XmlDocumentEx();
                xmlEx.Load(filePath);
            }
            catch (Exception)
            {
                xmlEx = null;
            }
            return xmlEx;
        }
    }
}
